package com.tledu.instrument.service.impl;

import com.tledu.common.utils.DateUtils;
import com.tledu.common.utils.SecurityUtils;
import com.tledu.instrument.domain.InstrumentInfo;
import com.tledu.instrument.domain.InstrumentLog;
import com.tledu.instrument.mapper.InstrumentInfoMapper;
import com.tledu.instrument.mapper.InstrumentLogMapper;
import com.tledu.instrument.service.IInstrumentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 仪器信息Service业务层处理
 *
 * @author longxf
 * @date 2024-08-28
 */
@Service
public class InstrumentInfoServiceImpl implements IInstrumentInfoService {


    @Autowired
    private InstrumentInfoMapper instrumentInfoMapper;

    @Autowired
    private InstrumentLogMapper instrumentLogMapper;

    /**
     * 查询仪器信息
     *
     * @param id 仪器信息主键
     * @return 仪器信息
     */
    @Override
    public InstrumentInfo selectInstrumentInfoById(Long id) {
        return instrumentInfoMapper.selectInstrumentInfoById(id);
    }

    /**
     * 查询仪器信息列表
     *
     * @param instrumentInfo 仪器信息
     * @return 仪器信息
     */
    @Override
    public List<InstrumentInfo> selectInstrumentInfoList(InstrumentInfo instrumentInfo) {
        return instrumentInfoMapper.selectInstrumentInfoList(instrumentInfo);
    }

    /**
     * 新增仪器信息
     *
     * @param instrumentInfo 仪器信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertInstrumentInfo(InstrumentInfo instrumentInfo) {
        //1 新增到仪器列表
        instrumentInfo.setCreateTime(DateUtils.getNowDate());
        instrumentInfo.setUpdateTime(DateUtils.getNowDate());
        instrumentInfo.setCreateUser(SecurityUtils.getUserId());
        instrumentInfo.setUpdateUser(SecurityUtils.getUserId());
        int i = instrumentInfoMapper.insertInstrumentInfo(instrumentInfo);
        //获得刚新增仪器的id
        int maxId = instrumentInfoMapper.selectInstrumentInfoId();

        //2 新增到日志表
        InstrumentLog instrumentLog = new InstrumentLog();
        instrumentLog.setInstrumentId((long) maxId);
        instrumentLog.setUpdateTime(DateUtils.getNowDate());
        instrumentLog.setCreateUser(SecurityUtils.getUserId());
        instrumentLog.setOperationType(0L);
        Date date = new Date();
        instrumentLog.setCreateTime(date);
        System.out.println(instrumentLog);
        int i1 = instrumentLogMapper.insertInstrumentLog(instrumentLog);
        //3.判断受影响的行数
        if (i > 0 && i1 > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 修改仪器信息
     *
     * @param instrumentInfo 仪器信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateInstrumentInfo(InstrumentInfo instrumentInfo) {
        //修改仪器信息
        instrumentInfo.setUpdateTime(DateUtils.getNowDate());
        int i = instrumentInfoMapper.updateInstrumentInfo(instrumentInfo);
        //新增到日志表
        InstrumentLog instrumentLog =new InstrumentLog();
        instrumentLog.setInstrumentId(instrumentInfo.getId());
        instrumentLog.setUpdateTime(DateUtils.getNowDate());
        instrumentLog.setCreateUser(SecurityUtils.getUserId());
        instrumentLog.setOperationType(1L);
        Date date = new Date();
        instrumentLog.setCreateTime(date);
        System.out.println(instrumentLog);
        int i1 = instrumentLogMapper.insertInstrumentLog(instrumentLog);
        //3.判断受影响的行数
        if (i > 0 && i1 > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 批量删除仪器信息
     *
     * @param ids 需要删除的仪器信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteInstrumentInfoByIds(Long[] ids) {
        return instrumentInfoMapper.deleteInstrumentInfoByIds(ids);
    }

    /**
     * 删除仪器信息信息
     *
     * @param id 仪器信息主键
     * @return 结果
     */
    @Override
    public int deleteInstrumentInfoById(Long id) {
        return instrumentInfoMapper.deleteInstrumentInfoById(id);
    }


    // 逻辑删除
    @Override
    public int updateShow(InstrumentInfo instrumentInfo) {
        //逻辑删除
        instrumentInfo.setUpdateTime(DateUtils.getNowDate());
        int i = instrumentInfoMapper.updateInstrumentLogic(instrumentInfo);
        //新增到日志表
        InstrumentLog instrumentLog =new InstrumentLog();
        instrumentLog.setInstrumentId(instrumentInfo.getId());
        instrumentLog.setUpdateTime(DateUtils.getNowDate());
        instrumentLog.setCreateUser(SecurityUtils.getUserId());
        instrumentLog.setOperationType(2L);
        Date date = new Date();
        instrumentLog.setCreateTime(date);
        System.out.println(instrumentLog);
        int i1 = instrumentLogMapper.insertInstrumentLog(instrumentLog);
        //3.判断受影响的行数
        if (i > 0 && i1 > 0) {
            return 1;
        } else {
            return 0;
        }
    }


}
